package com.uxwine.widget;

import com.uxwine.util.UxToast;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;
import android.widget.Scroller;

public class SlideView extends FrameLayout {
	
	private Scroller mScroller;
	private View mBaseView;
	private boolean mbSliding;
	private int mnState;
	private OnSlideListener mOnSlideListener;
	public static final int VIEW_OUT = 0;
	public static final int VIEW_IN = 1;

	public SlideView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	private void initView() {
		final Context context = getContext();
		mScroller = new Scroller(context);
		mbSliding = true;
	}
    
    @Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	public void computeScroll() {
		if (!mScroller.isFinished()) {
			if (mScroller.computeScrollOffset()) {
				int oldX = getScrollX();
				int oldY = getScrollY();
				int x = mScroller.getCurrX();
				int y = mScroller.getCurrY();
				if (oldX != x || oldY != y) {
					scrollTo(x, y);
				}
				// Keep on drawing until the animation has finished.
				invalidate();
			} else {
				//clearChildrenCache();
			}
		} else {
			//clearChildrenCache();
			if(mbSliding)
			{
				onSlidOver(getScrollX() == 0);	
				mbSliding = false;
			}
		}
	}

	@Override
	public void scrollTo(int x, int y) {
		super.scrollTo(x, y);
		postInvalidate();
	}
	
	public void setBaseView(View v)
	{
		mBaseView = v;
	}
	
	public void setOnSlideListener(OnSlideListener l)
	{
		mOnSlideListener = l;
	}
	
	public void smoothScrollTo(int dx) {
		mbSliding = true;
		int duration = 500;
		int oldScrollX = getScrollX();
		mScroller.startScroll(oldScrollX, getScrollY(), dx, getScrollY(),
				duration);
		invalidate();
	}
	
	public void toggle() {
		int menuWidth = mBaseView.getWidth();
		int oldScrollX = getScrollX();
		if (oldScrollX == 0) {
			smoothScrollTo(-menuWidth);
		} else if (oldScrollX == -menuWidth) {
			smoothScrollTo(menuWidth);
		}
	}
	
	public boolean isShow()
	{
		return (0 == getScrollX());
	}
	
	private void onSlidOver(boolean bIn)
	{
		if(null != mOnSlideListener)
		{
			mOnSlideListener.onSliding(this, bIn ? VIEW_IN : VIEW_OUT);
		}
	}

	private void enableChildrenCache() {
		final int count = getChildCount();
		for (int i = 0; i < count; i++) {
			final View layout = (View) getChildAt(i);
			layout.setDrawingCacheEnabled(true);
		}
	}

	private void clearChildrenCache() {
		final int count = getChildCount();
		for (int i = 0; i < count; i++) {
			final View layout = (View) getChildAt(i);
			layout.setDrawingCacheEnabled(false);
		}
	}
	
	public static interface OnSlideListener
	{
		public void onSliding(SlideView v, int state);
	}

}
